﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaskManager.Data
{
    public class SprintSqlServerRepository : ISprintRepository
    {
        public IList<Developer> GetDevelopers()
        {
            TaskManagerEntities taskManager = new TaskManagerEntities();
            return taskManager.Developers.ToList();
        }

        public void SaveDeveloper(Developer developer, bool delete = false)
        {
            TaskManagerEntities taskManager = new TaskManagerEntities();

            if (developer.DeveloperId > 0)
            {
                Developer persistedDeveloper = taskManager.Developers.First(d => d.DeveloperId == developer.DeveloperId);
                if (delete)
                {
                    taskManager.Developers.DeleteObject(developer);
                }
                else
                {
                    persistedDeveloper.Copy(developer);
                }
            }
            else
            {
                taskManager.AddToDevelopers(developer);
            }
            taskManager.SaveChanges();
        }

        public IList<Sprint> GetSprints()
        {
            TaskManagerEntities taskManager = new TaskManagerEntities();
            return taskManager.Sprints.ToList();
        }

        public string SaveSprint(Sprint sprint, bool delete = false)
        {
            string message = string.Empty;

            TaskManagerEntities taskManager = new TaskManagerEntities();

            if (sprint.SprintId > 0)
            {
                Sprint persistedSprint = taskManager.Sprints.First(s => s.SprintId == sprint.SprintId);
                if (delete)
                {
                    taskManager.Sprints.DeleteObject(sprint);
                }
                else
                {
                    // Does the sprint overlap with any other sprints?
                    Sprint overlappingSprint = taskManager.Sprints.Where(m => m.SprintId != sprint.SprintId && m.StartDate <= sprint.EndDate && m.EndDate >= sprint.StartDate).FirstOrDefault();

                    // If not...
                    if (overlappingSprint == null)
                    {
                        persistedSprint.Copy(sprint);
                    }
                    else
                    {
                        message = string.Format("The sprint start and end dates overlap with sprint {0} that has a start date of {1} and an end date of {2}.  Sprint dates cannot overlap.",
                            overlappingSprint.SprintId, overlappingSprint.StartDate.ToShortDateString(), overlappingSprint.EndDate.ToShortDateString());
                    }
                }
            }
            else
            {
                taskManager.AddToSprints(sprint);
            }

            if (message.Length == 0)
            {
                taskManager.SaveChanges();
            }

            return message;
        }

        public IList<Task> GetTasks()
        {
            TaskManagerEntities taskManager = new TaskManagerEntities();
            return taskManager.Tasks.ToList();
        }

        public IList<Task> GetTasks(Sprint sprint)
        {
            return sprint.Tasks.ToList();
        }

        public IList<Task> GetTasks(Developer developer)
        {
            return developer.Tasks.ToList();
        }

        public void SaveTask(Task task, bool delete)
        {
            TaskManagerEntities taskManager = new TaskManagerEntities();

            if (task.TaskId > 0)
            {
                Task persistedTask = taskManager.Tasks.First(t => t.TaskId == task.TaskId);
                if (delete)
                {
                    taskManager.Tasks.DeleteObject(persistedTask);
                }
                else
                {
                    persistedTask.Copy(task);
                }
            }
            else
            {
                taskManager.AddToTasks(task);
            }
            taskManager.SaveChanges();
        }
    }
}
